package org.ielts.profile;

import org.apache.commons.lang.StringUtils;
import org.ielts.domain.security.Role;
import org.ielts.service.security.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.ActionContext;
import com.yifeng.ZeroException;
import com.yifeng.actionlog.ActionLog;
import com.yifeng.actionlog.ActionLogUtils;
import com.yifeng.profile.IProfile;
import com.yifeng.profile.Profile;
import com.yifeng.security.authentication.Authentication;
import com.yifeng.security.groups.GroupCollection;
import com.yifeng.security.groups.Groups;
import com.yifeng.security.users.User;

public class IeltsProfile implements IProfile {

	@Autowired
	Authentication authentication;

	public User getUser() {
		String userId = authentication.getCurrentUser();
		if (isAuthenticated()) {
			User user = userService.findUser(userId);

			return user;
		}
		return null;
	}

	public String getUserId() {
		String userId = authentication.getCurrentUser();

		return userId;
	}

	public GroupCollection getGroups(String catalog) {
		String userName = authentication.getCurrentUser();

		return getGroups(userName, catalog);
	}

	@Autowired
	UserService userService;

	private GroupCollection getOrganization(String userName) {
		GroupCollection groupCollection = new GroupCollection(Profile.ORGANIZATION);
		return groupCollection;
	}

	private GroupCollection getRoles(String userName) {
		if (!isAuthenticated()) {
			return new GroupCollection(Profile.ROLE);
		}

		org.ielts.domain.security.User user = userService.findUser(userName);
		GroupCollection groupCollection = new GroupCollection(Profile.ROLE);
		if (user == null) {
			return groupCollection;
		} else {
			for (Role role : user.getRoles()) {
				role.setCatalog(Profile.ROLE);
				groupCollection.add(role);
			}

			return groupCollection;
		}
	}

	public boolean isAuthenticated() {
		String userId = authentication.getCurrentUser();
		return StringUtils.isNotBlank(userId) && !"null".equals(userId);
	}

	public String getTheme() {
		return "";
	}

	public ActionLog getActionLog() {
		ActionLog actionLog = ActionLogUtils.getBaseActionLog();

		GroupCollection orgGroups = getGroups(Profile.ROLE);
		GroupCollection companyGroups = getGroups(Profile.ORGANIZATION);

		Groups groups = new Groups();

		groups.addGroups(orgGroups);
		groups.addGroups(companyGroups);
		actionLog.setGroups(groups);

		return actionLog;
	}

	public GroupCollection getGroups(String userId, String catalog) {
		if (Profile.ROLE.equals(catalog)) {
			return getRoles(userId);
		} else if (Profile.ORGANIZATION.equals(catalog)) {
			return getOrganization(userId);
		}

		throw new ZeroException("没有实现的用户分组：" + catalog);
	}

	public void setCampus(String campus) {
		ActionContext.getContext().getSession().put("campus", campus);
	}

	public String getCampus() {
		return (String) ActionContext.getContext().getSession().get("campus");
	}
}
